package com.sailorj.service.impl;

import com.sailorj.entity.TbEnlist;
import com.sailorj.mapper.TbEnlistMapper;
import com.sailorj.mapper.TbPlanMapper;
import com.sailorj.service.TbEnlistService;
import com.sailorj.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class TbEnlistServiceImpl implements TbEnlistService {

    @Autowired
    public TbEnlistMapper tbEnlistMapper;
    @Autowired
    public TbPlanMapper tbPlanMapper;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public void addEnlist(TbEnlist tbEnlist) {
        /*
        查询该计划
         */
//        int id = Integer.valueOf(tbEnlist.getPlanId()).intValue();
//        System.out.println("id=="+id);
//        TbPlan tbPlan;
//        tbPlan   = tbPlanMapper.findPlanByid(id);
//        System.out.println("tbPlan=="+tbPlan.toString());
//        String num = tbPlan.getIsNum()+1;
//        tbPlanMapper.updatePlanSumById(id,num);
        tbEnlistMapper.insert(tbEnlist);
    }

    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    @Override
    public void updateEnlistById(TbEnlist tbEnlist) {
        tbEnlistMapper.updateByPrimaryKeySelective(tbEnlist);
    }

//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    @Override
//    public void deleteByPrimaryKey(int id) {
//        tbEnlistMapper.deleteByPrimaryKey(id);
//    }


    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public TbEnlist getEnlistById(int id) {
        return tbEnlistMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
    public PageBean getEnlistList(Integer page, Integer size) {
        if(page!=null&&size!=null){
            page=(page-1)*size;
        }
        List<TbEnlist> data = tbEnlistMapper.selectEnlistList(page,size);
        Long total=tbEnlistMapper.getTotal();
        PageBean bean = new PageBean();
        bean.setData(data);
        bean.setTotal(total);
        return bean;
    }

//
//    @Override
//    @Transactional(propagation = Propagation.REQUIRED)//事务回滚，发生错误不执行插入操作
//    public TbEnlist findByEnlistName(String name) {
//        return tbEnlistMapper.findPlanByName(name);
//    }
}
